﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ZK.BLL;
using ZK.Entity;
using ZK.MyControl;

namespace ZK.Client.WinForm
{
    public partial class FrmUserInfo : Form
    {
        public FrmUserInfo()
        {
            InitializeComponent();
        }

        BindingList<UserInfo> bindingList = new BindingList<UserInfo>();

        BindingSource bindingSource = new BindingSource();
        private void FrmUserInfo_Load(object sender, EventArgs e)
        {
            bindingSource.DataSource = bindingList;
            dataView1.DataSource = bindingSource;
            AddColumns();
            LoadData();
        }

        private void AddColumns()
        {
            dataView1.Columns.Add(DataGridColumnHelper.TextBoxColumnCreate(typeof(UserInfo) , nameof(UserInfo.UserId)));
            dataView1.Columns.Add(DataGridColumnHelper.TextBoxColumnCreate(typeof(UserInfo), nameof(UserInfo.Password)));
            dataView1.Columns.Add(DataGridColumnHelper.TextBoxColumnCreate(typeof(UserInfo), nameof(UserInfo.PersonName)));
            dataView1.Columns.Add(DataGridColumnHelper.TextBoxColumnCreate(typeof(UserInfo), nameof(UserInfo.PrintName)));
        }



        private void LoadData()
        {

           var list= UserInfoBLL.Instance.GetList();

            bindingList.Clear();

            foreach (var item in list)
            {
                bindingList.Add(item);
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            bindingList.Add(new UserInfo());
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            if (dataView1.CurrentCell != null && dataView1.CurrentCell.RowIndex > -1)
            {
                bindingList.RemoveAt(dataView1.CurrentCell.RowIndex);
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            UserInfoBLL.Instance.DeleteAll();
            
            if (UserInfoBLL.Instance.InsertList(bindingList.ToList()))
            {
                MessageBox.Show("保存成功");
            }
        }
    }
}
